package game;

import java.util.HashMap;

import hibernateUtil.Playerinfo;
import io.ClientSession;
import io.OperationResponse;
import io.UseMoneyParams;
import io.UseMoneyType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import define.ServerDefine;
import encouragement.EncourageAll;
import MemoryDB.YuanTable;
import Public.PlayerAttributesFunction;
import Public.PlayerPublicFun;
import Public.PublicFunction;
import Public.RefObject;
import player.Player;
import protocol.OpCode;
import protocol.ParameterType;
import protocol.ReturnCode;
import protomsg.Player.PlayerInfoMsg2;
import protomsg.Rpc.ServerID;
import util.CommonTool;
import util.PlayerUtil;

/**
 * �ͻ���������Ҫ��ʾ��Ǯ��Ѫʯ��ʵ�ʼ۸����������з������ڴ˴���
 * 
 * @author Snova-Red
 * 
 */

public class ShowAllMONEY {
	public static int[] blood = { 20, 25, 29, 32, 34 };

	protected static final Logger log = LoggerFactory
			.getLogger(ShowAllMONEY.class);

	/**
	 * ������ȡԤ�ж�
	 * 
	 * @param operationResponse
	 * @param session
	 */
	public static void strength(OperationResponse operationResponse,
			ClientSession session) {

		try {
			short useMoneyType = (Short) operationResponse.Parameters
					.get(UseMoneyParams.UseMoneyType);
			int num1 = (Integer) operationResponse.Parameters
					.get(UseMoneyParams.Num1);

			ShowStrength(useMoneyType, num1, (Player) session.getClient(),
					session);

		} catch (Exception e) {
			log.info("����Ԥ�ж���bug:" + e.toString());
		}

	}

	public static void ShowStrength(Short useType, int num1, Player player,
			ClientSession session) {
		// double aDouble = Double.parseDouble(Integer.toString(num1));
		// double strength = 0;
		int tili = 0;
		int beforeTili = 0;
		if (player.myTable != null) {
			beforeTili = player.myTable.getPower();// ��ǰ����ҵ�����
			YuanTable tableVIP = PlayerPublicFun.pubPlayerService
					.get(player.myTable.getServing());
			int Big = Integer.parseInt(tableVIP.getItem(0).getRowItem()
					.get("HP").getYuanColumnText());// ȡ������ҵ��������
			java.util.HashMap<Short, Object> dicSend = new HashMap<Short, Object>();
			dicSend.put((short) 0, useType);
			switch (useType) {
			case ServerDefine.NormalDungeon:

				tili = -10;
				break;

			case ServerDefine.EliteDungeon:
				// strength = Clamp(aDouble * 0.34 + 10, 15.00, 30.00);
				// tili = new Double(strength)
				// .intValue()*-1;
				tili = -25;
				break;
			case ServerDefine.ShadowDemonWin:
				tili = 5;
				// �����ҵ�����С��������������ޣ�����ȡ��vip��HP�ֶ�
				if (beforeTili <= Big) {
					// �����ҵ�����+�����������ڵ�����ҵ������������������ӵ�����ҵ����ޣ����򰴸����������ӵ����������
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 5;
					}
					
				} else {
					// �����ҵ��������ڴ���ҵ���������򲻸�����Ҽ�������
					tili = 0;
				}

				break;
			case ServerDefine.ShadowDemonlose:
				tili = 2;
				if (beforeTili <= Big) {
					// �����ҵ�����+�����������ڵ�����ҵ������������������ӵ�����ҵ����ޣ����򰴸����������ӵ����������
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 2;
					}

				} else {
					// �����ҵ��������ڴ���ҵ���������򲻸�����Ҽ�������
					tili = 0;
				}
				break;
			case ServerDefine.PowerSolution:
				tili = 100;
				break;
			case ServerDefine.PowerOnline:
				tili = 15;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 15;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.EatFish:
				tili = num1;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = num1;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.Raids:
				tili = -10;
				break;
			case ServerDefine.InviteShadowDemon:
				tili = -10;
				break;
			case ServerDefine.LoginPower:

				float num = (float) num1;
				float a = num / 3600 * 10;

				tili = (int) a;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = (int)a;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.EveryDayAim:
				tili = 30;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 30;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.shengji:
				tili = 5;
				// if(beforeTili<=Big){
				// if(beforeTili+tili>=Big){
				// tili=Big-beforeTili;
				// }else{
				// tili = 5;
				// }
				//
				// }else{
				// tili=0;
				// }
				break;

			}
			dicSend.put((short) 1, tili);
			if (player.myTable.getPower() < (tili * -1)) {
				OperationResponse operationResponse2 = new OperationResponse(
						OpCode.ShowStrength, dicSend, ReturnCode.No, "");
				session.SendOperationResponse(operationResponse2);
				return;
			}
			OperationResponse operationResponse2 = new OperationResponse(
					OpCode.ShowStrength, dicSend, ReturnCode.Yes, "");
			session.SendOperationResponse(operationResponse2);

		}

	}

	public static void receive(OperationResponse operationResponse,
			ClientSession session) {
		try {
			short useMoneyType = (Short) operationResponse.Parameters
					.get(UseMoneyParams.UseMoneyType);
			int num1 = (Integer) operationResponse.Parameters
					.get(UseMoneyParams.Num1);
			int num2 = (Integer) operationResponse.Parameters
					.get(UseMoneyParams.Num2);
			String itemID = (String) operationResponse.Parameters
					.get(UseMoneyParams.ItemID);
			Show(useMoneyType, num1, num2, itemID,
					(Player) session.getClient(), session);

		} catch (Exception e) {
			log.info("Ѫʯ��ǮԤ�ж���bug:" + e.toString());
		}

	}

	public static void Show(Short useType, int num1, int num2, String itemID,
			Player player, ClientSession session) {
		int cBlood = 0;
		int[] costMoney = null;
		int cGold = 0;
		int cMarrowIron = 0;
		int cMarrowGold = 0;
		int HeroStone = 0;
		if (player.myTable != null) {
			java.util.HashMap<Short, Object> dicSend = new HashMap<Short, Object>();
			dicSend.put(UseMoneyParams.UseMoneyType, useType);
			switch (useType) {
			case UseMoneyType.WashAttributePoints:
				int time = 0;
				time = player.myTable.getWashAttributePoints();
				if (time == 0) {
					cBlood = 200;
				} else {
					if (time >= 4) {
						cBlood = 600;
					} else {
						cBlood = 200 + time * 100;
					}
				}

				break;

			case UseMoneyType.SaveBranch:
				cBlood = 600;
				break;
			case UseMoneyType.YesBuyCard:
				break;
			case UseMoneyType.YesDoCD:
				cBlood = num1;
				break;
			case UseMoneyType.Sell:

				costMoney = PlayerAttributesFunction.GetCostMoney(itemID, num1);
				cGold = costMoney[0] * (-1) + costMoney[1] * 500 * (-1);
				if (cGold * -1 > 20000) {
					cGold = -20000;
				}
				break;
			case UseMoneyType.Buy:
				break;
			case UseMoneyType.YesOpenCangKu:
				cBlood = num1 * 10;
				break;
			case UseMoneyType.CostGold:
				cGold = num1;
				break;
			case UseMoneyType.OpenSpreeItemAsID:
				costMoney = SendPack.GetCostMoneyInTablePacks(itemID);
				cGold = costMoney[0] * (-1);
				cBlood = costMoney[1] * (-1);
				cMarrowIron = costMoney[2];
				cMarrowGold = costMoney[3];
				break;
			case UseMoneyType.YesOpenPackage:
				cBlood = num1 * 10;
				break;
			case UseMoneyType.YAddGold:
				cGold = num1 * (-1);
				break;
			case UseMoneyType.YAddBlood:
				cBlood = num1 * (-1);
				break;
			case UseMoneyType.YesPVP8Times:
				cBlood = 5;
				break;
			case UseMoneyType.YesPVPTimes:
				cBlood = 5;
				break;
			case UseMoneyType.Resurrection2:
				cGold = new Double((Double.parseDouble(Integer
						.toString(num1 / 2)) + 0.8) * 2000).intValue();
				break;
			case UseMoneyType.Resurrection3:
				cBlood = 100;
				break;
			case UseMoneyType.mmm31:
				cBlood = -60;
				break;
			case UseMoneyType.mmm32:
				cBlood = -200;
				break;
			case UseMoneyType.mmm33:
				cBlood = -900;
				break;
			case UseMoneyType.mmm34:
				cBlood = -3500;
				break;
			case UseMoneyType.UpDateOneSkill:
				cGold = num1 * num1 * 100;
				break;
			case UseMoneyType.SKILLDeviation:
				cBlood = num1 * 15;
				break;
			case UseMoneyType.UpDateOneSoulItemSkill:
				cGold = num1 * num1 * num2 + 99;
				// cGold = num1 * num2 * 200;
				break;
			case UseMoneyType.ButtonBuildSoulAndDigest:
				cBlood = blood[num1 - 1];
				if (itemID.equals("1")) {
					cBlood += 100;
				}

				// SetBuildSDCostMoney();
				// cGold = BuildSDCostMoney[num1];
				break;
			case UseMoneyType.SoulAndDigestGold:
				cGold = (150 * num1 * num2 / 5) * (-1);
				break;
			case UseMoneyType.NumDigestButtonsPlus:
				cBlood = 5;
				break;
			case UseMoneyType.PlusBottle:
				cGold = num1 * num1 * num2 + 99;
				break;
			case UseMoneyType.ApplyPickup1:
				break;
			case UseMoneyType.ApplyPickup2:
				break;
			case UseMoneyType.TaskDone:
				break;
			case UseMoneyType.SpendingReturns11:
				cGold = num1 * 200;
				break;
			case UseMoneyType.RaidsStart:
				cBlood = 10;
				break;
			case UseMoneyType.CostRaidsNow:
				cBlood = num1;
				break;
			case UseMoneyType.Cost:
				break;
			case UseMoneyType.UpLevelBottle:
				cBlood = num1 * 10;
				break;
			case UseMoneyType.PlusBottle1:
				cGold = num1 * num2 * 5 + 300;
				break;

			case UseMoneyType.FullBottle:
				cBlood = 10;
				break;
			case UseMoneyType.YesReturn:
				cBlood = 2;
				break;
			case UseMoneyType.YesManufacture:
				cBlood = 20;
				break;
			case UseMoneyType.YesSoul:
				cBlood = 30;
				break;
			case UseMoneyType.YesGem:
				cBlood = 50;
				break;
			case UseMoneyType.YesDuplicate:
				cBlood = 50;
				break;
			case UseMoneyType.YesResetPVP:
				cBlood = 20;
				break;
			case UseMoneyType.yesCook:
				cBlood = 2;
				break;
			case UseMoneyType.YesBuy:
				cBlood = 50;
				break;
			case UseMoneyType.Getreward:

				cGold = (num1 * 20 * (int) (50 * Math.random() + 50) / 100)
						* (-1);
				break;
			case UseMoneyType.BenefitsSalaries: {

				cGold = -1 * EncourageAll.benefitsSalaries
						* player.myTable.getPlayerLevel();
				int guildGold = -1 * EncourageAll.benefitsGuild
						* player.myTable.getGuildContribution();
				cGold = cGold + guildGold;

			}
				break;
			case UseMoneyType.BenefitsRank: {
				cGold = -1 * EncourageAll.benefitsRank
						* player.myTable.getRank();
			}
				break;
			case UseMoneyType.BenefitsGuild: {

				cGold = -1 * EncourageAll.benefitsGuild
						* player.myTable.getGuildContribution();
			}
				break;
			case UseMoneyType.TipsOpenCard:
				cBlood = DoneCard.Clamp(player.mapLevelDondCard / 8, 2, 10);

				cBlood += cBlood * (num2 + 2) / 4;
				cBlood *= 1;
				if (num2 + 1 == 2) {
					double Gold = 0.8 * (player.mapLevelDondCard * 240) + 200;
					cGold = new Double(Gold).intValue();
					cBlood = 0;
				}
				if (player.mapLevelDondCard == 5) {
					cGold = cGold * 3 / 2;
					cBlood = cBlood * 3 / 2;
				}
				break;
			case UseMoneyType.TipsEquepmentBuild:
				cBlood = num1;
				break;
			
			case UseMoneyType.TipsBuyStoreClient:

				break;
			case UseMoneyType.TipsEquepmentHole:
				switch (num1) {
				case 0:
					cBlood = 5;
					break;
				case 1:
					cBlood = 5;
					break;
				case 2:
					cBlood = 10;
					break;
				case 3:
					cBlood = 20;
					break;
				}

				break;
//			case UseMoneyType.TipsYesUpdateDS:// Ŀǰû���κδ������ǿͻ����ڴ���
//				soul = num1 * num2 * 20;

//				break;
			case UseMoneyType.TipsHuntingUp:

				if(num2==1){
					float level=(float)num1;
					cBlood=(int)level/5*2+2;
					
				}else if (num2==2) {
					float level=(float)num1;
					cBlood=(int)(10+4*(level/5*2+2));
					
				} else if(num2==3){
					cBlood=10;
				}
				
				break;
//			case UseMoneyType.TipsResurrection2:
//				cGold = new Double((Double.parseDouble(Integer
//						.toString(num1 / 2)) + 0.8) * 2000).intValue();
//				cBlood=100;
//				break;
//		

			}
			dicSend.put(UseMoneyParams.GoldNum, cGold);
			dicSend.put(UseMoneyParams.BloodNum, cBlood);
			dicSend.put(ParameterType.MarrowIron, cMarrowIron);
			dicSend.put(ParameterType.MarrowGold, cMarrowGold);
			dicSend.put(ParameterType.HeroStone, HeroStone);
			OperationResponse operationResponse2 = new OperationResponse(
					OpCode.ShowAllMONEY, dicSend, ReturnCode.Yes, "");
			session.SendOperationResponse(operationResponse2);
		}

	}

	public static void ChangePower(OperationResponse operationResponse,
			ClientSession session) {

		try {
			short useMoneyType = (Short) operationResponse.Parameters
					.get(UseMoneyParams.UseMoneyType);
			int num1 = (Integer) operationResponse.Parameters
					.get(UseMoneyParams.Num1);

			kouTili(useMoneyType, num1, ((Player) session.getClient()).myTable, session);

		} catch (Exception e) {
			log.info("ʵ�ʿ�������bug:" + e.toString());
		}

	}

	public static void kouTili(Short useType, int num1, Playerinfo playerinfo,
			ClientSession session) {
		Player player=((Player) session.getClient());

		int tili = 0;
		int beforeTili = 0;
		if (playerinfo != null) {
			beforeTili = playerinfo.getPower();// ��ǰ����ҵ�����
			YuanTable tableVIP = PlayerPublicFun.pubPlayerService
					.get(playerinfo.getServing());
			int Big = Integer.parseInt(tableVIP.getItem(0).getRowItem()
					.get("HP").getYuanColumnText());// ȡ������ҵ��������
			java.util.HashMap<Short, Object> dicSend = new HashMap<Short, Object>();
			dicSend.put((short) 111, useType);
			switch (useType) {
			case ServerDefine.NormalDungeon:

				tili = -10;
				break;

			case ServerDefine.EliteDungeon:
				// strength = Clamp(aDouble * 0.34 + 10, 15.00, 30.00);
				// tili = new Double(strength)
				// .intValue()*-1;
				tili = -25;
				break;
			case ServerDefine.ShadowDemonWin:
				tili = 5;
				// �����ҵ�����С��������������ޣ�����ȡ��vip��HP�ֶ�
				if (beforeTili <= Big) {
					// �����ҵ�����+�����������ڵ�����ҵ������������������ӵ�����ҵ����ޣ����򰴸����������ӵ����������
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 5;
					}

				} else {
					// �����ҵ��������ڴ���ҵ���������򲻸�����Ҽ�������
					tili = 0;
				}
				player.yingmoTILI=tili;
				break;
			case ServerDefine.ShadowDemonlose:
				tili = 2;
				if (beforeTili <= Big) {
					// �����ҵ�����+�����������ڵ�����ҵ������������������ӵ�����ҵ����ޣ����򰴸����������ӵ����������
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 2;
					}

				} else {
					// �����ҵ��������ڴ���ҵ���������򲻸�����Ҽ�������
					tili = 0;
				}
				break;
			case ServerDefine.PowerSolution:
				tili = 100;
				break;
			case ServerDefine.PowerOnline:
				tili = 15;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 15;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.EatFish:
				tili = num1;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = num1;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.Raids:
				tili = -10;
				break;
			case ServerDefine.InviteShadowDemon:
				tili = -10;
				break;
			case ServerDefine.LoginPower:
				
				float num = (float) num1;
				float a = num / 3600 * 10;
				tili = (int) a;
				//�����ǰ��ҵ�������С���������
				if (beforeTili < Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = (int) a;
					}

				} else {
					tili = 0;
				}
				player.addTILI=tili;
				
				return;
//				break;
			case ServerDefine.EveryDayAim:
				tili = 30;
				if (beforeTili <= Big) {
					if (beforeTili + tili >= Big) {
						tili = Big - beforeTili;
					} else {
						tili = 30;
					}

				} else {
					tili = 0;
				}
				break;
			case ServerDefine.shengji:
				tili = 5;
				 if(beforeTili<=Big){
				 if(beforeTili+tili>=Big){
				 tili=Big-beforeTili;
				 }else{
				 tili = 5;
				 }
				
				 }else{
				 tili=0;
				 }
				break;

			}
			

			dicSend.put((short) 100, tili);
		
			if (playerinfo.getPower() < (tili * -1)) {
					OperationResponse operationResponse2 = new OperationResponse(
							OpCode.DeductStrength, dicSend, ReturnCode.No, "");
					session.SendOperationResponse(operationResponse2);
					return;
				
			}

			playerinfo.setPower(beforeTili + tili);
			
			
		
				java.util.HashMap<String, String> tempParms = new HashMap<String, String>();
				tempParms.put("Power", playerinfo.getPower().toString());

				String[] strKey = null;
				String[] strValue = null;
				RefObject<String[]> refStrKey = new RefObject<String[]>(strKey);
				RefObject<String[]> refStrValue = new RefObject<String[]>(strValue);
				PublicFunction.SetTable(tempParms, refStrKey, refStrValue,
						UseMoneyParams.TableKey, UseMoneyParams.shortTableSql,
						dicSend);
				strKey = refStrKey.argvalue;
				strValue = refStrValue.argvalue;

				OperationResponse operationResponse2 = new OperationResponse(
						OpCode.DeductStrength, dicSend, ReturnCode.Yes, "");
				session.SendOperationResponse(operationResponse2);
		}

	}

	public static void ShowTranining(OperationResponse operationResponse,
			ClientSession session) {
		java.util.HashMap<Short, Object> dicSend = new HashMap<Short, Object>();
		Player player = (Player)session.getClient();
		
		int NonPoint = player.myTable.getNonPoint();
		int Level = player.myTable.getPlayerLevel();
		int gold = 0;
		int useMoney=0;
		int otherYuSe=0;
		int useBlood=0;
		for(int i =Level;i>0;i--)
		{
			gold += i*2+6;
			
		}
		useMoney =PlayerUtil.Clamp(((gold-NonPoint)/6)*40+500,500,4800);
		int viplev =Integer.parseInt(player.myTable.getServing()); 
		if(viplev==1||(viplev>1&&viplev<4))
		{
//			yuse = 24;
			otherYuSe = 24;
			useBlood = 20;
//			lent = 1;
		}else if(viplev>=4)
		{
//			yuse = 48;
			otherYuSe = 48;
//			lent = 2;
			useBlood = 40;
		}else 
		{
//			yuse = 12;
			otherYuSe = 12;
			useBlood = 12;
//			lent = 1;
		}

		dicSend.put((short)1, useMoney);
		dicSend.put((short)2, 12);
		dicSend.put((short)3, 20);
		dicSend.put((short)4, 40);
		dicSend.put((short)5, useBlood);
		dicSend.put((short)6, otherYuSe);
		dicSend.put((short)7, 1);
		dicSend.put((short)8, 4);
		OperationResponse operationResponse2 = new OperationResponse(
				OpCode.ShowTraining, dicSend, ReturnCode.Yes, "");
		session.SendOperationResponse(operationResponse2);
	

	}

	public static Double Clamp(Double value, Double min, Double max) {
		if (value >= max) {
			return max;
		} else if (value <= min) {
			return min;
		} else {
			return value;
		}
	}
	public static void addtili(OperationResponse operationResponse,
			ClientSession session) {
		Player player=((Player) session.getClient());
		player.myTable.setPower(player.myTable.getPower() + player.addTILI);

		java.util.HashMap<Short, Object> dicSend = new HashMap<Short, Object>();

		java.util.HashMap<String, String> tempParms = new HashMap<String, String>();
		dicSend.put((short) 100, player.addTILI);
		dicSend.put((short) 111, (short)10);

		tempParms.put("Power", player.myTable.getPower().toString());

		String[] strKey = null;
		String[] strValue = null;
		RefObject<String[]> refStrKey = new RefObject<String[]>(strKey);
		RefObject<String[]> refStrValue = new RefObject<String[]>(strValue);
		PublicFunction.SetTable(tempParms, refStrKey, refStrValue,
				UseMoneyParams.TableKey, UseMoneyParams.shortTableSql,
				dicSend);
		strKey = refStrKey.argvalue;
		strValue = refStrValue.argvalue;

		OperationResponse operationResponse2 = new OperationResponse(
				OpCode.DeductStrength, dicSend, ReturnCode.Yes, "");
		session.SendOperationResponse(operationResponse2);
		player.addTILI=0;

	}
}
